package com.newsee.remote.common.utils.tools;

import org.apache.commons.lang3.StringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liangzhi.chen
 * @description 融创xml封装工具类
 * @date 2019/7/24 上午 11:35
 **/
public class XmlBuildUtil {

    /**
     * header_began
     */
    private final static String COMMON_HEADER_BEGAN = " <soapenv:Header>\n" + "<v1:commonHeader>\n";

    /**
     * header_end
     */
    private final static String COMMON_HEADER_END = "</v1:commonHeader>\n" + "</soapenv:Header>\n";

    /**
     * BODY_BEGAN
     */
    private final static String COMMON_BODY_BEGAN = " <soapenv:Body>\n";

    /**
     * BODY_END
     */
    private final static String COMMON_BODY_END = " </soapenv:Body>\n";


    /**
     * 封装xml方法请求
     *
     * @return String -> Xml
     */
    public static String makeXml(String startStr, String method, String systemID, String account, String password, int i, String methedNo, String startTime, String endTime) {
        Date date = new Date();
        String now = DateUtils.format(date, "yyyyMMddHHmmsssss");

        Map<String, String> header = new HashMap<>(6);
        header.put("v1:BIZTRANSACTIONID", "BS-WYWZ-P_" + methedNo + now);
        header.put("v1:COUNT", "");
        header.put("v1:CONSUMER", systemID);
        header.put("v1:SRVLEVEL", "");
        header.put("v1:ACCOUNT", account);
        header.put("v1:PASSWORD", password);
        Map<String, String> body = new HashMap<>(5);

        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        Date yesterday = c.getTime();
        String beginDate;
        if (StringUtils.isNotBlank(startTime)) {
            beginDate = startTime;
        } else {
            beginDate = DateUtils.format(yesterday, "yyyy-MM-dd HH:mm:ss.sss");
        }
        String endDate;
        if (StringUtils.isNotBlank(endTime)) {
            endDate = endTime;
        } else {
            c.setTime(date);
            endDate = DateUtils.format(date, "yyyy-MM-dd HH:mm:ss.sss");
        }

//        beginDate = "2018-10-23 00:00:00.000";
//        endDate = "2018-10-24 00:00:00.000";
        body.put("pub:beginDate", beginDate);
        body.put("pub:endDate", endDate);
        body.put("pub:pageNo", String.valueOf(i));
        body.put("pub:pageRowNo", "50");
        body.put("pub:systemID", systemID);

        StringBuilder sb = new StringBuilder();
        sb.append(startStr);

        //header
        sb.append(COMMON_HEADER_BEGAN);
        handleParams(header, sb);
        sb.append(COMMON_HEADER_END);

        //body
        sb.append(COMMON_BODY_BEGAN);
        sb.append("<pub:").append(method).append(">\n");
        sb.append("<pub:queryDto").append(">\n");
        handleParams(body, sb);
        sb.append("</pub:queryDto").append(">\n");
        sb.append("</pub:").append(method).append(">\n");
        sb.append(COMMON_BODY_END);
        sb.append("</soapenv:Envelope>");
        return sb.toString();
    }

    private static void handleParams(Map<String, String> todoHeader, StringBuilder sb) {
        for (String key : todoHeader.keySet()) {
            String value = todoHeader.get(key);
            if (StringUtils.isNotEmpty(value)) {
                sb.append("<").append(key).append(">");
                //特殊字符需要转换
                sb.append(value.replaceAll("&", "&amp;"));
                sb.append("</").append(key).append(">");
            } else {
                sb.append("<").append(key).append("/>");
            }
            sb.append("\n");
        }
    }


}
